package com.scott.crm.student.service.impl;

import com.scott.base.utils.AESUtil;
import com.scott.base.utils.GetDateTime;
import com.scott.crm.clazz.bean.ClazzBean;
import com.scott.crm.student.bean.DictionaryBean;
import com.scott.crm.student.bean.StudentBean;
import com.scott.crm.student.bean.StudentDto;
import com.scott.crm.student.dao.DictionaryDao;
import com.scott.crm.student.dao.StudentDao;
import com.scott.crm.student.entity.StudentEntity;
import com.scott.crm.student.service.StudentService;
import com.scott.crm.user.bean.UserBean;
import com.scott.crm.user.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private DictionaryDao dictionaryDao;




    @Override
    public ClazzBean findClassByStudentID(Integer studentId) {
        return studentDao.findClassByStudentID(studentId);
    }

    @Override
    public UserBean findTeacherByClazzID(Integer classId) {
        return studentDao.findTeacherByClazzID(classId);
    }

    @Override
    public StudentEntity findStudentMessageByStudentId(Integer studentId) {
        return studentDao.findStudentMessageByStudentId(studentId);
    }


    public Integer addStudent(StudentBean studentBean)  {
            studentBean.setCreateDate(GetDateTime.nowTime());
            studentBean.setUpdateDate(GetDateTime.nowTime());
            return studentDao.insert(studentBean);

    }



    public Integer updateUserMessage(StudentEntity studentEntity) throws Exception {
        UserBean userBean=new UserBean();
        StudentBean studentBean=new StudentBean();

        // 解密拿到userId
        String token=studentEntity.getToken();
        String userIds = AESUtil.decrypt(token, AESUtil.KEY);
        Integer userId=Integer.parseInt(userIds);

        userBean.setId(userId);
        userBean.setName(studentEntity.getName());
        userBean.setPhone(studentEntity.getPhone());
        userBean.setEmail(studentEntity.getEmail());

        studentBean.setId(userId);
        studentBean.setEducation(studentEntity.getEducation());

        studentDao.updateByPrimaryKeySelective(studentBean);
        userDao.updateByPrimaryKeySelective(userBean);
        return null;
    }

    public List<DictionaryBean> findEducation() {
        DictionaryBean dictionary=new DictionaryBean();
        dictionary.setParentId(700);
        List<DictionaryBean> select = dictionaryDao.select(dictionary);
        return select;
    }

    @Override
    public StudentDto findClassAndTeacherByStudentId(String token) throws Exception {
        //解密userId
        String userIds = AESUtil.decrypt(token, AESUtil.KEY);
        Integer userId=Integer.parseInt(userIds);

        //查找班级
        ClazzBean classBean = studentDao.findClassByStudentId(userId);
        Integer classId = classBean.getId();
        UserBean teacherBean = studentDao.findTeacherByClassId(classId);

        //查找老师
        StudentDto studentDto=new StudentDto();
        studentDto.setClazzBean(classBean);
        studentDto.setUserBean(teacherBean);

        return studentDto;
    }

//    @Override
//    public UserBean findTeacherByClassId(Integer classId) {
//
//        return studentDao.findTeacherByClassId(classId);
//    }


}
